﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Projet_WPF.DataAccess
{
    class User
    {
        public List<Model.User> GetListUser()
        {
            try
            {
                ServiceUser.ServiceUserClient service = new ServiceUser.ServiceUserClient();
                List<Model.User> list = new List<Model.User>();

                foreach (var user in service.GetListUser())
                {
                    Model.User u = Model.User.CreateUser(user.Login, user.Pwd, user.Name, user.Firstname, user.Picture, user.Role, user.Connected);
                    list.Add(u);
                }

                return list;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public Model.User GetUser(string login)
        {
            try
            {
                ServiceUser.ServiceUserClient service = new ServiceUser.ServiceUserClient();
                ServiceUser.User user = service.GetUser(login);
                Model.User u = Model.User.CreateUser(user.Login, user.Pwd, user.Name, user.Firstname, user.Picture, user.Role, user.Connected);
                return u;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public bool AddUser(Model.User user)
        {
            try
            {
                ServiceUser.ServiceUserClient service = new ServiceUser.ServiceUserClient();
                ServiceUser.User u = new ServiceUser.User();

                u.Login = user.Login;
                u.Pwd = user.Pwd;
                u.Name = user.Name;
                u.Firstname = user.Firstname;
                u.Picture = user.Picture;
                u.Role = user.Role;
                u.Connected = user.Connected;

                return service.AddUser(u);
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool DeleteUser(string login)
        {
            try
            {
                ServiceUser.ServiceUserClient service = new ServiceUser.ServiceUserClient();

                return service.DeleteUser(login);
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool Connect(string login, string pwd)
        {
            try
            {
                ServiceUser.ServiceUserClient service = new ServiceUser.ServiceUserClient();

                return service.Connect(login, pwd);
            }
            catch (Exception)
            {
                return false;
            }
        }

        public void Disconnect(string login)
        {
            try
            {
                ServiceUser.ServiceUserClient service = new ServiceUser.ServiceUserClient();

                service.Disconnect(login);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public string GetRole(string login)
        {
            try
            {
                ServiceUser.ServiceUserClient service = new ServiceUser.ServiceUserClient();

                return service.GetRole(login);
            }
            catch (Exception)
            {
                return "";
            }
        }
    }
}
